package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.CartDao;
import com.qf.xiaomi.pojo.CartUnion;
import com.qf.xiaomi.utils.DruidDButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Created by 马英超 on 2022/8/18 17:37
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class CartDaoImpl implements CartDao {

    /**
     * 购物车数据处理
     * @param id
     * @param num
     * @param uid
     * @return
     */

    @Override
    public boolean dealCartData(int id,int num,int uid) {

        //根据传入的数据查询数据库内是否存在这个商品，如果存在，则根据传入的产品数量增加
        String sql="SELECT  COUNT(0) FROM  tb_cart t WHERE t.id=? AND t.pid=?";
        String  sql1="INSERT INTO  tb_cart(id,pid,num) VALUES(?,?,?) ";
        String  sql2="UPDATE tb_cart t SET t.num=num+? WHERE t.id=? AND pid=?";
        QueryRunner queryRunner=new QueryRunner();
         Object query=null;
        try {
            query = queryRunner.query(DruidDButils.getDruidConn(), sql, new ScalarHandler(),uid, id );
            if(query!=null){
                final int i = Integer.parseInt(String.valueOf(query));
                if(i==0){
                    //如果i=0，数据库内不存在商品，则将商品信息查询后插入购物车表内
                    final int addProductsResult = queryRunner.update(DruidDButils.getDruidConn(),sql1, uid, id, num);
                    if(addProductsResult>0)
                        return true;
                    return false;
                }else{
                    //如果i!=0，数据库内存在商品，则更新商品内的数量
                    final int updateProductsResult = queryRunner.update(DruidDButils.getDruidConn(),sql2, num, uid, id);
                    if(updateProductsResult>0)
                        return true;
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据用户id和商品id查询购物车内是否已经存在了商品信息
     * @param id
     * @param uid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean queryCart(int id, int uid) throws SQLException {
        String sql="SELECT  COUNT(0) FROM  tb_cart t WHERE t.id=? AND t.pid=?";
        QueryRunner queryRunner=new QueryRunner();
        Number  number= (Number) queryRunner.query(DruidDButils.getDruidConn(), sql, new ScalarHandler(),uid, id );
        final int intValue = number.intValue();
        if(intValue!=0)
            return true;
        return false;
    }

    /**
     * 向购物车内插入商品信息
     * @param id
     * @param num
     * @param uid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean addCart(int id, int num, int uid) throws SQLException {
        String  sql="INSERT INTO  tb_cart(id,pid,num) VALUES(?,?,?) ";
        QueryRunner queryRunner=new QueryRunner();
        final int addProductsResult = queryRunner.update(DruidDButils.getDruidConn(),sql, uid, id, num);
        if (addProductsResult>0)
            return true;
        return false;
    }

    /**
     * 更新购物车内已存在商品信息
     * @param id
     * @param num
     * @param uid
     * @return
     * @throws SQLException
     */

    @Override
    public boolean updateCart(int id, int num, int uid) throws SQLException {
        String  sql="UPDATE tb_cart t SET t.num=num+? WHERE t.id=? AND pid=?";
        QueryRunner queryRunner=new QueryRunner();
        final int updateProductsResult = queryRunner.update(DruidDButils.getDruidConn(),sql, num, uid, id);
        if (updateProductsResult>0)
            return true;
        return false;
    }

    /**
     * 使用多表联查的方式联查商品、购物车数据
     * @param uid
     * @return
     */
    @Override
    public List<CartUnion> queryCartList(String uid) {
        //参数处理
        int userId=Integer.parseInt(uid);
       // String sql="SELECT a.id,a.name,a.price FROM tb_goods_kz a WHERE a. id IN(SELECT pid FROM tb_cart b WHERE b.id=?)";
       /*多表联查效率慢*/
         String sql="SELECT a.id,b.id as uid,a.name,a.price,b.num,b.pid FROM tb_goods_kz a,tb_cart b WHERE a.id=b.pid AND b.id=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final List<CartUnion> cartUnions = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanListHandler<>(CartUnion.class), userId);
             return cartUnions;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //删除购物车
    @Override
    public int  deleteCart(String cid, String uid) {
        //参数处理
        int pid=Integer.parseInt(cid);
        int id=Integer.parseInt(uid);
        String sql="delete from tb_cart  where pid=? and id=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final int update = queryRunner.update(DruidDButils.getDruidConn(), sql, pid,id);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
//根据用户名清空用户购物车
    @Override
    public boolean deleteCartByUserId(int uid) {
        String sql="delete from tb_cart where id=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final int clearResult = queryRunner.update(DruidDButils.getDruidConn(), sql, uid);
            if (clearResult>=1)
                return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
